<?php
namespace  app\admin\controller;
use think\Controller;
use think\Db;
use think\Request;
use app\admin\model\Admin_model;
Class Advertisers extends  Controller{
    function __construct(Request $request = null)
    {
        parent::__construct($request);
        if(session('user')) {
            $user=session('user');
            $admin_model=new Admin_model();
            $fun=$admin_model->get_rolefunction($user['role']);
            $this->assign('fun',$fun);
            $this->assign('active','个人中心');
        }else{
            $this->redirect('index/login/index');
        }
    }
    function  advertisers_list(){
        $this->assign('title','广告主列表');
        return $this->fetch();
    }
   function  get_advlist(){
       $user=session('user');
        $where=array();
        if ($user['role']!=0){
            $c=Db::name('child_user')->where('attr_user_id',$user['id'])->select();
            $attr_user_id[]=$user['id'];
            foreach ($c as $v)
            {
                $u=Db::name('user')->field('id') ->where('username',$v['name'])->find();
                $attr_user_id[]=$u['id'];
            }
            $where['attr_user_id']=['in' ,$attr_user_id];
        }
        if($_GET['search']){
            $where['name']=array('like','%'.$_GET['search'].'%');
        }
        $total=Db::name('advertisers')->where($where)->count();
        $rows=Db::name('advertisers')->limit($_GET['offset'],$_GET['limit'])->where($where)->order($_GET['sort'],$_GET['order'])->select();

        foreach ($rows as &$row){
            $uid=Db::name('user')->field('id') ->where('username',$row['name'])->find();
            $attr_user=Db::name('user') ->where('id',$row['attr_user_id'])->find();
            $row['attr_user_rname']=$attr_user['username'];
            $row['operate']='<a onclick="reset_pwd('.$uid['id'].')" class="btn btn-link">重置密码</a> <a onclick="recharge('.$uid['id'].')" class="btn btn-link">充值</a>
            <a href="#" class="btn btn-link">充优惠券</a> <a href="#" class="btn btn-link">模拟登录</a>';
        }
        echo  json_encode(array('total'=>$total,'rows'=>$rows));
   }
    function  add_advertisers(){
        $user=session('user');
        if(request()->isPost()) {
              $data=input('post.');
              unset($data['adv_pwd_check']);
              unset($data['file']);

              $adv=Db::name('advertisers')->whereOr('name',$data['name'])->whereOr('advertisers_name',$data['advertisers_name'])->select();
              $zh=Db::name('user')->where('username',$data['name'])->select();
              if(!empty($adv) ||  !empty($zh)){
                  return array('code'=>0,'msg'=>'用户名或广告主名称已被使用');
              }else{
                  $data['attr_user_id']=$user['id'];
                  $id=Db::name('advertisers')->insertGetId($data);
                  $user=array('username'=>$data['name'],'password'=>$data['adv_pwd'],'role'=>GGZ);
                  $uid=Db::name('user')->insert($user);
                  if($id&&$uid){
                      return array('code'=>1,'msg'=>'新增成功','url'=>url('advertisers_list'));
                  }else{
                      return array('code'=>0,'msg'=>'新增失败');
                  }

              }

        }
        $industry= Db::name('industry')->where('level',1)->select();
        $this->assign('industry',$industry);
        $this->assign('title','添加广告主');
        return $this->fetch();
    }
    //给广告主充钱
    //获取广告主账户余额
    public function  get_user_amount(){
        $user_id=$_GET['user_id'];
        $amout=Db::name('user_balance')->where('user_id',$user_id)->find();
        if(empty($amout)){
            $user=Db::name('user')->where('id',$user_id)->find();
            $am['username']=$user['username'];
            $am['user_id']=$user['id'];
            $id=Db::name('user_balance')->insertGetId($am);
            if($id){
                $amout=Db::name('user_balance')->where('user_id',$user_id)->find();
                return array('code'=>1,'user'=>session('user'),'amount'=>$amout);
            }else{
                return array('code'=>0,'amount'=>'');
            }
        }else{
            return array('code'=>1,'user'=>session('user'),'amount'=>$amout);
        }
    }
    //充值账户
    public  function  recharge(){
        $data=input('post.');
        if($data){
            Db::startTrans();
            try{
                //给广告主充钱
                $adv_amount=Db::name('user_balance')->where('user_id',$data['user_id'])->find();
                $adv_amount['cash_balance']=$adv_amount['cash_balance']+$data['recharge_amount'];
                $adv_row=Db::name('user_balance')->where('id',$adv_amount['id'])->update($adv_amount);
                //给归属代理商扣钱
                $amount=Db::name('user_balance')->where('user_id',$data['operation_user_id'])->find();
                $amount['cash_balance']=$amount['cash_balance']-$data['recharge_amount'];
                $row=Db::name('user_balance')->where('id',$amount['id'])->update($amount);

                $id=Db::name('recharge_record')->insertGetId($data);
                if($row && $id && $adv_row){
                    Db::commit();
                    return array('code' =>1,'msg'=>'充值成功');
                }else{
                    Db::rollback();
                    return array('code' =>0,'msg'=>'充值失败');
                }
            } catch (\Exception $e) {
                Db::rollback();
                return array('code' =>-1,'msg'=>'充值失败');
            }
        }else{
            return array('code' =>-2,'msg'=>'丢失参数');
        }
    }
}